<template>
    <div class="login-container">
        <el-form ref="ruleFormRef" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
            label-position="left">
            <div class="title-container">
                <h3 class="title">管理系统</h3>
            </div>

            <el-form-item prop="userName" class="login-inp">
                <span class="svg-container">
                    <el-icon>
                        <User />
                    </el-icon>
                </span>
                <el-input ref="username" v-model="loginForm.userName" placeholder="用户名" name="username" type="text"
                    tabindex="1" autocomplete="on" />
            </el-form-item>

            <el-form-item prop="password" class="login-inp">
                <span class="svg-container">
                    <el-icon>
                        <Lock />
                    </el-icon>
                </span>
                <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
                    placeholder="密码" name="password" tabindex="2" autocomplete="on" @keyup.enter="handleLogin" />
                <span class="show-pwd" @click="showPwd">
                    <el-icon>
                        <Hide v-if="passwordType === 'password'" />
                        <View v-else-if="passwordType === 'text'" />
                    </el-icon>
                </span>
            </el-form-item>

            <el-form-item prop="rember">
                <el-checkbox v-model="loginForm.rember" label="记住密码" size="large" />
            </el-form-item>

            <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px"
                @click.prevent="handleLogin">
                登录</el-button>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { login } from '../../api/user'
import { ElMessage, type FormInstance } from 'element-plus';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { User, Lock } from '@element-plus/icons-vue'

const ruleFormRef = ref<FormInstance>()
const loginForm = ref({
    userName: 'admin',
    password: '',
    rember: false
})
const loginRules = ref({
    userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 5, message: '用户名不能少于5个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, message: '密码不能少于5个字符', trigger: 'blur' }
    ]
})
const passwordType = ref<'password' | 'text'>('password')
const loading = ref(false)
const router = useRouter()

function showPwd() {
    if (passwordType.value === 'password') {
        passwordType.value = 'text'
    } else {
        passwordType.value = 'password'
    }
}
async function handleLogin() {
    await ruleFormRef.value?.validate((valid) => {
        if (valid) {
            loading.value = true
            login(loginForm.value).then(res => {
                console.log(res);
                if (res.code === 1) {
                    ElMessage({
                        message: '登录成功',
                        type: 'success',
                        duration: 1500,
                        onClose() {
                            router.push('/')
                        },
                    })
                } else {
                    ElMessage({
                        message: res.message,
                        type: 'error',
                        duration: 1500
                    })
                }
            }).finally(() => {
                loading.value = false
            })
        }
    })
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

.login-container {
    .el-input {
        height: 47px;
        width: 85%;

        .el-input__wrapper,
        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            box-shadow: none;
        }

        input {
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .login-inp {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 30px 50px 10px;
        margin: 120px auto;
        overflow: hidden;
        background-color: #44505f;
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .el-checkbox {
        margin-bottom: 10px;
    }
}
</style>
